﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WoSoft.PACS.Data.Doctor;
using DevComponents.DotNetBar;
using WoSoft.PACS.Data.Models;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    public partial class FrmDoctorInfo : FrmBase
    {
        //部门ID
        private string _departmentID = string.Empty;
        //医生id
        private string _ID = string.Empty;

        DoctorData doctorData = new DoctorData();

        public FrmDoctorInfo()
        {
            InitializeComponent();
        }

        public FrmDoctorInfo(string departmentID, string id)
        {
            InitializeComponent();

            _departmentID = departmentID;
            _ID = id;
        }

        #region 加载数据
        private void FrmDoctorInfo_Load(object sender, EventArgs e)
        {
            //加载学历
            this.LoadEducationList(this.cbEducation);

            //加载性别
            this.LoadSexList(this.cbSex);

            //加载民族
            this.LoadNationList(this.cbNation);

            //加载拼音码
            this.SetSpellCodeControl(this.tbName, this.tbCode);

            //加载ID数据
            if (!string.IsNullOrEmpty(this._ID))
            {
                DoctorModel doctorModel = doctorData.GetModelByID(this._ID);
                if (doctorModel != null)
                {
                    this.tbName.Text=doctorModel.Name;
                    this.tbCode.Text=doctorModel.Code;
                    this.cbSex.SelectedValue=doctorModel.Sex;
                    this.cbNation.SelectedValue=doctorModel.Nation;
                    this.cbEducation.SelectedValue=doctorModel.Education;
                    this.dateTimeInput1.Value=doctorModel.Birthday;
                    this.tbEmail.Text=doctorModel.Email;
                    this.tbTel.Text=doctorModel.Tel;
                    this.cbLevel.SelectedValue=doctorModel.Level;
                }
            }
        }
        #endregion

        //关闭
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //确定 
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.tbName.Text.Trim().Length <= 0)
            {
                this.errorProvider1.SetError(this.tbName, "姓名是必填项目");
                return;
            }

            //获取界面数据
            var doctorModel = new PACS.Data.Models.DoctorModel()
            {
                //ID = Guid.NewGuid().ToString(),
                DepartmentID = _departmentID,
                Name = this.tbName.Text.Trim(),
                Code = this.tbCode.Text.Trim(),
                Sex = this.cbSex.SelectedValue.ToString(),
                Nation = this.cbNation.SelectedValue.ToString(),
                Education = Convert.ToInt32(this.cbEducation.SelectedValue),
                Birthday = this.dateTimeInput1.Value,
                Email = this.tbEmail.Text.Trim(),
                Tel = this.tbTel.Text.Trim(),
                NameCode = "",
                Level = 0
            };

            if (string.IsNullOrEmpty(this._ID))//新增
            {
                doctorModel.ID = Guid.NewGuid().ToString();
                doctorData.Insert(doctorModel);
                MessageBoxEx.Show("新建医护人员信息成功!", "创建档案", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else//修改
            {
                doctorModel.ID = this._ID;
                doctorData.Update(doctorModel);
                MessageBoxEx.Show("修改医护人员信息成功!", "创建档案", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.DialogResult = DialogResult.OK;
        }
    }
}
